let loading
function getDate(){
    return new Promise((resolve, reject) => {
        getZmWhereType(4).then(res=>{
            resolve(res)
        })
    })
}
let list_data = [{title:1},{title:2}]
window.list =function(){

    return {

        list_data,
        playIconXuanZhuan:false,
        audio:document.createElement('audio'),
        playState:0, //控制播放状态的属性 0 单曲循环  1 列表循环
        animate:null, //动画对象
        currentMxText:0, //设置呼吸频率 单位为 s
        mxTextArr:[],
        animateDuration:6,
        huxiText:'点我',
        cishu:'0',
        tanchuangShow:false, //设置弹窗是否显示
        timeId:null,
        textarea_text:'',
        async initData(){

            await anonymouslogin()

            let load = layer.load()

            this.list_data = await getDate()

            this.list_data.forEach(i=>{
                i['anim_play'] = true
                i['activePlay_p'] = false
                i['activePlay'] = false
            })
            console.log(this.list_data)
            layer.close(load)

            this.audioConfig()

        },
        playAd(path,id,url){

            if(id == this.audio.currentid){
                if(this.audio.paused){
                    this.audio.play()
                }else{
                    this.audio.pause()
                }
                return
            }

            this.audio.src = url
            this.audio.currentid = id

            loading = layer.msg('音频加载中', {
                 icon: 16
                ,shade: 0.01
            });

            setTimeout(()=>{

                this.audio.play()

            },300)

        },
        /**
         * 通过ICON控制音频播放的
         */
        playKongzhi(e){

            console.log(e);

            if(this.audio.src==''){
                layer.msg('请点击屏幕左上侧列表图标，选择默想音频')
           }else{

               this.list_data.map(i=>{

                   if(i._id == this.audio.currentid){

                       console.log(i._id);
                       console.log(i.activePlay);
                       console.log(i.activePlay_p);
                       if(i.activePlay){
                           this.audio.pause()
                       }
                       if(i.activePlay_p){

                            this.audio.play()
                       }
                   }
               })
           }
       },
        audioConfig(){

            this.audio.onerro = ()=>{
                layer.msg('音频加载失败！')
            }

            this.audio.oncanplay = ()=>{
                layer.close(loading)
            }

            this.audio.onplay = ()=>{

                $("#playIcon").addClass('xuanzhuan')

                this.playIconXuanZhuan = true

                this.list_data.forEach(i=>{

                    if(this.audio.currentid == i._id){
                        i.activePlay = true
                    }else{
                        i.activePlay = false
                    }
                    i.activePlay_p = false
                })

            }

            this.audio.onended = ()=>{
                console.log("播放结束")
                if(this.playState==0){
                    this.audio.play()
                }else if(this.playState==1){

                    let cid = this.audio.currentid
                    let bool = false
                    this.list_data.forEach((i)=>{
                        if(!bool){
                            if(i._id == cid){
                                bool = true
                            }
                        }else{
                            this.playAd(i.path,i._id,i.dmtUrl.adUrl)
                            bool = false
                        }

                    })
                }

            }

            this.audio.onpause = ()=>{

                let id = this.audio.currentid

                this.playIconXuanZhuan = false

                this.list_data.forEach(i=>{
                    if(i._id == id){
                        i.activePlay_p = true
                    }else{
                        i.activePlay_p = false
                    }

                })

            }
        },
        playstate(){

            let a = this.playState+1
            this.playState = a%2
        },
        animatePlay($refs){

            let d = $refs.yuan
            let keyfase = [
                    {
                        transform:'scale(0.7)',
                        background: 'radial-gradient(circle at  70% 70%,  rgba(207,231,250,.8) 0%,rgba(0,98,156,.1) 100%)',
                    }
                    ,{
                            transform:'scale(0.8)',
                            background: 'radial-gradient(circle at 70% 70%,  rgba(207,231,250,.8) 0%,rgba(0,98,156,.2) 100%)',
                    }
                    ,{
                            transform:'scale(0.9)',
                            background: 'radial-gradient(circle at 70%  70%,  rgba(207,231,250,.8) 0%,rgba(0,88,156,.3) 100%)',
                    }
                    ,{
                            transform:'scale(1)',
                            background: 'radial-gradient(circle at 70% 70%,  rgba(207,231,250,.8) 0%,rgba(0,78,156,.4) 100%)',
                    }
                    ,{
                            transform:'scale(1.1)',
                            background: 'radial-gradient(circle at 70% 70%,  rgba(207,231,250,.8) 0%,rgba(0,68,156,.5) 100%)',
                    }
                    ,{
                            transform:'scale(1.2)',
                            background: 'radial-gradient(circle at 70% 70%,  rgba(207,231,250,.8) 0%,rgba(0,58,156,.6) 100%)',
                    }
            ]

            if(!this.animate){

                this.animate = d.animate(keyfase,{
                    duration:this.animateDuration * 1000,
                    iterations:'Infinity',
                    easing:'linear',
                    direction:'alternate',
                })

                this.timeId = setInterval(()=>{

                    let currentTime =parseInt(this.animate.currentTime / 1000)
                    if(currentTime % this.animateDuration == 0 && this.animate.playState == 'running'){
                        this.mx_textKongzhi()
                    }
                    let currentDb = currentTime % (this.animateDuration *2)
                    if(currentDb < this.animateDuration){
                        this.huxiText = '吸'
                    }else{
                        this.huxiText = '呼'
                    }
                    if(currentDb == 0){
                        this.cishu++
                    }

                },1000)

            }else{


                let playState = this.animate.playState
                if(playState == 'running'){
                    this.animate.pause()

                }
                if(playState == 'paused'){
                    this.animate.play()
                }

            }
        },
        mx_textKongzhi(){
            this.currentMxText++
            if(this.currentMxText >= this.mxTextArr.length){
                this.currentMxText = 0
            }
        },
        settings(){

            this.tanchuangShow = !this.tanchuangShow

            if(this.tanchuangShow){

                if(this.animate){
                    this.animate.cancel()
                    this.animate = null
                    clearInterval(this.timeId)
                }
                this.textarea_text==''
            }else{

                if(this.textarea_text!==''){
                    this.mxTextArr = this.textarea_text.split('\n')
                }

            }

        },
        jianFun:function (){
            this.animateDuration--
        }
    }
}



